package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientClientDetailService;
import com.mdd.admin.validate.ClientClientDetailCreateValidate;
import com.mdd.admin.validate.ClientClientDetailUpdateValidate;
import com.mdd.admin.validate.ClientClientDetailSearchValidate;
import com.mdd.admin.vo.ClientClientDetailListedVo;
import com.mdd.admin.vo.ClientClientDetailDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientClientDetail;
import com.mdd.common.mapper.ClientClientDetailMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 【请填写功能名称】实现类
 * @author LikeAdmin
 */
@Service
public class ClientClientDetailServiceImpl implements IClientClientDetailService {
        
    @Resource
    ClientClientDetailMapper clientClientDetailMapper;

    /**
     * 【请填写功能名称】列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientClientDetailListedVo>
     */
    @Override
    public PageResult<ClientClientDetailListedVo> list(PageValidate pageValidate, ClientClientDetailSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientClientDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        clientClientDetailMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:clientId@client_id:int",
            "=:registerTime@register_time:str",
            "=:registerMoney@register_money:str",
            "=:registerType@register_type:str",
            "=:unitWebsite@unit_website:str",
            "like:headOfUnitName@head_of_unit_name:str",
            "=:headOfUnitSex@head_of_unit_sex:str",
            "=:headOfUnitPhone@head_of_unit_phone:str",
            "=:headOfUnitNumber@head_of_unit_number:str",
            "=:headOfUnitPost@head_of_unit_post:str",
            "=:headOfUnitEmail@head_of_unit_email:str",
            "=:mainProducts@main_products:str",
            "=:mainBusinessBelongs@main_business_belongs:str",
            "=:upstreamProducts@upstream_products:str",
            "=:downstreamProducts@downstream_products:str",
            "=:isStorageForRecord@is_storage_for_record:str",
            "=:isAdditionalDeduction@is_additional_deduction:str",
            "=:isParkRegistration@is_park_registration:str",
            "like:parkRegistrationName@park_registration_name:str",
            "=:isDoctoralResearchStation@is_doctoral_research_station:str",
            "=:doctorPitStop@doctor_pit_stop:str",
            "=:totalEquipment@total_equipment:str",
            "=:futureTotal@future_total:str",
            "=:amountOfInvestment@amount_of_investment:str",
            "=:ofInvestmentBeSpecific@of_investment_be_specific:str",
            "=:ofInvestmentOthers@of_investment_others:str",
            "=:isCloudMigration@is_cloud_migration:str",
            "=:isDemonstrationProject@is_demonstration_project:str",
            "=:constructionOfLand@construction_of_land:str",
            "=:listingStatus@listing_status:str",
            "=:goToMarketProcess@go_to_market_process:str",
            "=:placeOfListing@place_of_listing:str",
            "=:amountOfFinancingNeed@amount_of_financing_need:str",
            "=:plannedFinancingMethod@planned_financing_method:str",
            "=:revenueFromSales@revenue_from_sales:str",
            "=:totalAssets@total_assets:str",
            "=:netWorth@net_worth:str",
            "=:netProfit@net_profit:str",
            "=:netProfitMargin@net_profit_margin:str",
            "=:totalLiabilities@total_liabilities:str",
            "=:research andDevelopmentExpenses@research and_development_expenses:str",
            "=:proportion:str",
            "=:amountOfTaxPaid@amount_of_tax_paid:int",
            "=:assetLiabilityRatio@asset_liability_ratio:str",
            "=:bankLoan@bank_loan:str",
            "=:volumeOfExports@volume_of_exports:str",
            "=:totalNumber@total_number:int",
            "=:socialSecurity@social_security:int",
            "=:personalIncomeTax@personal_income_tax:str",
            "=:personnelNumber@personnel_number:int",
            "=:phd:str",
            "=:master:str",
            "=:phdLg@phd_lg:str",
            "=:masterLg@master_lg:str",
            "=:undergraduate:str",
            "=:specialistDepartment@specialist_department:str",
            "=:undergraduateLg@undergraduate_lg:str",
            "=:specialistDepartmentLg@specialist_department_lg:str",
            "=:seniorProfessionaTitle@senior_professiona_title:str",
            "=:intermediateProfessionalTitle@intermediate_professional_title:str",
            "=:juniorProfessionalTitle@junior_professional_title:str",
            "=:returnFromStudyAbroad@return_from_study_abroad:str",
            "like:productName@product_name:str",
            "=:annualOutputValue@annual_output_value:str",
        });

        IPage<ClientClientDetail> iPage = clientClientDetailMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientClientDetailListedVo> list = new LinkedList<>();
        for(ClientClientDetail item : iPage.getRecords()) {
            ClientClientDetailListedVo vo = new ClientClientDetailListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 【请填写功能名称】详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientClientDetail
     */
    @Override
    public ClientClientDetailDetailVo detail(Integer id) {
        ClientClientDetail model = clientClientDetailMapper.selectOne(
                new QueryWrapper<ClientClientDetail>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientClientDetailDetailVo vo = new ClientClientDetailDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 【请填写功能名称】新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientClientDetailCreateValidate createValidate) {
        ClientClientDetail model = new ClientClientDetail();
        model.setClientId(createValidate.getClientId());
        model.setRegisterTime(createValidate.getRegisterTime());
        model.setRegisterMoney(createValidate.getRegisterMoney());
        model.setRegisterType(createValidate.getRegisterType());
        model.setUnitWebsite(createValidate.getUnitWebsite());
        model.setHeadOfUnitName(createValidate.getHeadOfUnitName());
        model.setHeadOfUnitSex(createValidate.getHeadOfUnitSex());
        model.setHeadOfUnitPhone(createValidate.getHeadOfUnitPhone());
        model.setHeadOfUnitNumber(createValidate.getHeadOfUnitNumber());
        model.setHeadOfUnitPost(createValidate.getHeadOfUnitPost());
        model.setHeadOfUnitEmail(createValidate.getHeadOfUnitEmail());
        model.setMainProducts(createValidate.getMainProducts());
        model.setMainBusinessBelongs(createValidate.getMainBusinessBelongs());
        model.setUpstreamProducts(createValidate.getUpstreamProducts());
        model.setDownstreamProducts(createValidate.getDownstreamProducts());
        model.setIsStorageForRecord(createValidate.getIsStorageForRecord());
        model.setIsAdditionalDeduction(createValidate.getIsAdditionalDeduction());
        model.setIsParkRegistration(createValidate.getIsParkRegistration());
        model.setParkRegistrationName(createValidate.getParkRegistrationName());
        model.setIsDoctoralResearchStation(createValidate.getIsDoctoralResearchStation());
        model.setDoctorPitStop(createValidate.getDoctorPitStop());
        model.setTotalEquipment(createValidate.getTotalEquipment());
        model.setFutureTotal(createValidate.getFutureTotal());
        model.setAmountOfInvestment(createValidate.getAmountOfInvestment());
        model.setOfInvestmentBeSpecific(createValidate.getOfInvestmentBeSpecific());
        model.setOfInvestmentOthers(createValidate.getOfInvestmentOthers());
        model.setIsCloudMigration(createValidate.getIsCloudMigration());
        model.setIsDemonstrationProject(createValidate.getIsDemonstrationProject());
        model.setConstructionOfLand(createValidate.getConstructionOfLand());
        model.setListingStatus(createValidate.getListingStatus());
        model.setGoToMarketProcess(createValidate.getGoToMarketProcess());
        model.setPlaceOfListing(createValidate.getPlaceOfListing());
        model.setAmountOfFinancingNeed(createValidate.getAmountOfFinancingNeed());
        model.setPlannedFinancingMethod(createValidate.getPlannedFinancingMethod());
        model.setRevenueFromSales(createValidate.getRevenueFromSales());
        model.setTotalAssets(createValidate.getTotalAssets());
        model.setNetWorth(createValidate.getNetWorth());
        model.setNetProfit(createValidate.getNetProfit());
        model.setNetProfitMargin(createValidate.getNetProfitMargin());
        model.setTotalLiabilities(createValidate.getTotalLiabilities());
        model.setResearchAndDevelopmentExpenses(createValidate.getResearchAndDevelopmentExpenses());
        model.setProportion(createValidate.getProportion());
        model.setAmountOfTaxPaid(createValidate.getAmountOfTaxPaid());
        model.setAssetLiabilityRatio(createValidate.getAssetLiabilityRatio());
        model.setBankLoan(createValidate.getBankLoan());
        model.setVolumeOfExports(createValidate.getVolumeOfExports());
        model.setTotalNumber(createValidate.getTotalNumber());
        model.setSocialSecurity(createValidate.getSocialSecurity());
        model.setPersonalIncomeTax(createValidate.getPersonalIncomeTax());
        model.setPersonnelNumber(createValidate.getPersonnelNumber());
        model.setPhd(createValidate.getPhd());
        model.setMaster(createValidate.getMaster());
        model.setPhdLg(createValidate.getPhdLg());
        model.setMasterLg(createValidate.getMasterLg());
        model.setUndergraduate(createValidate.getUndergraduate());
        model.setSpecialistDepartment(createValidate.getSpecialistDepartment());
        model.setUndergraduateLg(createValidate.getUndergraduateLg());
        model.setSpecialistDepartmentLg(createValidate.getSpecialistDepartmentLg());
        model.setSeniorProfessionaTitle(createValidate.getSeniorProfessionaTitle());
        model.setIntermediateProfessionalTitle(createValidate.getIntermediateProfessionalTitle());
        model.setJuniorProfessionalTitle(createValidate.getJuniorProfessionalTitle());
        model.setReturnFromStudyAbroad(createValidate.getReturnFromStudyAbroad());
        model.setProductName(createValidate.getProductName());
        model.setAnnualOutputValue(createValidate.getAnnualOutputValue());
        clientClientDetailMapper.insert(model);
    }

    /**
     * 【请填写功能名称】编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientClientDetailUpdateValidate updateValidate) {
        ClientClientDetail model = clientClientDetailMapper.selectOne(
                new QueryWrapper<ClientClientDetail>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setClientId(updateValidate.getClientId());
        model.setRegisterTime(updateValidate.getRegisterTime());
        model.setRegisterMoney(updateValidate.getRegisterMoney());
        model.setRegisterType(updateValidate.getRegisterType());
        model.setUnitWebsite(updateValidate.getUnitWebsite());
        model.setHeadOfUnitName(updateValidate.getHeadOfUnitName());
        model.setHeadOfUnitSex(updateValidate.getHeadOfUnitSex());
        model.setHeadOfUnitPhone(updateValidate.getHeadOfUnitPhone());
        model.setHeadOfUnitNumber(updateValidate.getHeadOfUnitNumber());
        model.setHeadOfUnitPost(updateValidate.getHeadOfUnitPost());
        model.setHeadOfUnitEmail(updateValidate.getHeadOfUnitEmail());
        model.setMainProducts(updateValidate.getMainProducts());
        model.setMainBusinessBelongs(updateValidate.getMainBusinessBelongs());
        model.setUpstreamProducts(updateValidate.getUpstreamProducts());
        model.setDownstreamProducts(updateValidate.getDownstreamProducts());
        model.setIsStorageForRecord(updateValidate.getIsStorageForRecord());
        model.setIsAdditionalDeduction(updateValidate.getIsAdditionalDeduction());
        model.setIsParkRegistration(updateValidate.getIsParkRegistration());
        model.setParkRegistrationName(updateValidate.getParkRegistrationName());
        model.setIsDoctoralResearchStation(updateValidate.getIsDoctoralResearchStation());
        model.setDoctorPitStop(updateValidate.getDoctorPitStop());
        model.setTotalEquipment(updateValidate.getTotalEquipment());
        model.setFutureTotal(updateValidate.getFutureTotal());
        model.setAmountOfInvestment(updateValidate.getAmountOfInvestment());
        model.setOfInvestmentBeSpecific(updateValidate.getOfInvestmentBeSpecific());
        model.setOfInvestmentOthers(updateValidate.getOfInvestmentOthers());
        model.setIsCloudMigration(updateValidate.getIsCloudMigration());
        model.setIsDemonstrationProject(updateValidate.getIsDemonstrationProject());
        model.setConstructionOfLand(updateValidate.getConstructionOfLand());
        model.setListingStatus(updateValidate.getListingStatus());
        model.setGoToMarketProcess(updateValidate.getGoToMarketProcess());
        model.setPlaceOfListing(updateValidate.getPlaceOfListing());
        model.setAmountOfFinancingNeed(updateValidate.getAmountOfFinancingNeed());
        model.setPlannedFinancingMethod(updateValidate.getPlannedFinancingMethod());
        model.setRevenueFromSales(updateValidate.getRevenueFromSales());
        model.setTotalAssets(updateValidate.getTotalAssets());
        model.setNetWorth(updateValidate.getNetWorth());
        model.setNetProfit(updateValidate.getNetProfit());
        model.setNetProfitMargin(updateValidate.getNetProfitMargin());
        model.setTotalLiabilities(updateValidate.getTotalLiabilities());
        model.setResearchAndDevelopmentExpenses(updateValidate.getResearchAndDevelopmentExpenses());
        model.setProportion(updateValidate.getProportion());
        model.setAmountOfTaxPaid(updateValidate.getAmountOfTaxPaid());
        model.setAssetLiabilityRatio(updateValidate.getAssetLiabilityRatio());
        model.setBankLoan(updateValidate.getBankLoan());
        model.setVolumeOfExports(updateValidate.getVolumeOfExports());
        model.setTotalNumber(updateValidate.getTotalNumber());
        model.setSocialSecurity(updateValidate.getSocialSecurity());
        model.setPersonalIncomeTax(updateValidate.getPersonalIncomeTax());
        model.setPersonnelNumber(updateValidate.getPersonnelNumber());
        model.setPhd(updateValidate.getPhd());
        model.setMaster(updateValidate.getMaster());
        model.setPhdLg(updateValidate.getPhdLg());
        model.setMasterLg(updateValidate.getMasterLg());
        model.setUndergraduate(updateValidate.getUndergraduate());
        model.setSpecialistDepartment(updateValidate.getSpecialistDepartment());
        model.setUndergraduateLg(updateValidate.getUndergraduateLg());
        model.setSpecialistDepartmentLg(updateValidate.getSpecialistDepartmentLg());
        model.setSeniorProfessionaTitle(updateValidate.getSeniorProfessionaTitle());
        model.setIntermediateProfessionalTitle(updateValidate.getIntermediateProfessionalTitle());
        model.setJuniorProfessionalTitle(updateValidate.getJuniorProfessionalTitle());
        model.setReturnFromStudyAbroad(updateValidate.getReturnFromStudyAbroad());
        model.setProductName(updateValidate.getProductName());
        model.setAnnualOutputValue(updateValidate.getAnnualOutputValue());
        clientClientDetailMapper.updateById(model);
    }

    /**
     * 【请填写功能名称】删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientClientDetail model = clientClientDetailMapper.selectOne(
                new QueryWrapper<ClientClientDetail>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        clientClientDetailMapper.delete(new QueryWrapper<ClientClientDetail>().eq("id", id));
    }

}
